package view;
import data.Admin;
import data.User;
import data.Product;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class AdminUi extends Admin{
    // 构造函数，用于初始化管理员的用户名和密码
    public AdminUi(String name, String password) {
        super(name, password);
    }


    // 管理员登录
    public static String login(ArrayList<Admin>list2) {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入管理员用户名");
        String username=sc.next();
        boolean flag= Admin.contains2(list2,username);
        if(!flag){
            System.out.println("管理员账户"+username+"未注册，请先注册再登录");
            return "0";
        }
        System.out.println("请输入密码");
        String password=sc.next();
        Admin AdminInfo=new Admin(username,password);

        boolean result=checkAdminInfo(list2,AdminInfo);
        if(result){
            System.out.println("登录成功，欢迎使用购物系统");
            return "1";
        }
        else{
            System.out.println("登录失败，用户名或密码错误");
            return "0";
        }
    }
    // 修改管理员登录密码
    public static void  modifyAdminPassword(ArrayList<Admin>list){
        for (Admin admin : list) {
            Scanner sc = new Scanner(System.in);

            System.out.println("请输入原密码");
            String password = sc.next();
            if (Objects.equals(password, admin.getPassword())) {
                System.out.println("请输入新密码");
                String password1 = sc.next();
                admin.setPassword(password1);
                System.out.println("修改成功！");
            }
        }

    }
    // 重置用户密码
    public static void resetUserPassword(ArrayList<User>list) {

        for (User user : list) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要重置密码的用户名");
            String username = sc.next();
            if (Objects.equals(username, user.getUsername())) {
                System.out.println("请输入新密码");
                String password1 = sc.next();
                user.setPassword(password1);
                System.out.println("重置密码成功！");
            } else {
                System.out.println("重置密码失败，用户名错误");
            }
            break;
        }
    }
    // 打印用户列表
    public static void printUserList(ArrayList<User> list){
        if(list.isEmpty()){
            System.out.println("当前无用户信息，请添加后再查询");
            return;
        }
        System.out.println("客户id\t\t用户名\t用户级别\t用户注册时间\t客户消费金额\t用户手机号\t邮箱");
        for (User user : list) {
            System.out.println(STR."\{user.getid()}\t\{user.getUsername()}\t\{user.getLeval()}\{user.getRegisterTime()}\t\{user.getTotalAmount()}\t\{user.getPhoneNumber()}\t\{user.getEmail()}");
        }
    }
    // 删除用户
    public static void removeUser(ArrayList<User>list) {
        Scanner in = new Scanner(System.in);
        System.out.println("您选择了删除客户功能");
        System.out.println("请选择输入Username或phone：1-Username，2-phone");
        String choice = in.next();
        if (Objects.equals(choice, "1")) {
            System.out.println("请输入name：");
            String name = in.next();
            boolean flag = false;

            for (int i = 0; i < list.size(); i++)  {
                User user=list.get(i);
                if (name.equals(user.getUsername())) {

                    System.out.println("客户用户名为" + user.getUsername()+"的会员信息为：" + STR."\{user.getid()}\t\{user.getUsername()}\t\{user.getLeval()}\{user.getRegisterTime()}\t\{user.getTotalAmount()}\t\{user.getPhoneNumber()}\t\{user.getEmail()}" );
                    System.out.println("请问确认要删除吗？y or n");
                    String choice_input = in.next();
                    if (choice_input.equals("y")) {
                        list.remove(user);
                        System.out.println("删除成功！");
                        flag = true;
                        break;
                    }
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                System.out.println("未找到该客户");
            }
        } else if (Objects.equals(choice, "2")) {
            System.out.println("请输入phone：");
            String phone = in.next();

            boolean flag = false;
            for (int i = 0; i < list.size(); i++){
                User user=list.get(i);
                if (phone.equals(user.getPhoneNumber())) {
                    System.out.println("phone为" + user.getPhoneNumber() + "的会员信息为：" + STR."\{user.getid()}\t\{user.getUsername()}\t\{user.getLeval()}\{user.getRegisterTime()}\t\{user.getTotalAmount()}\t\{user.getPhoneNumber()}\t\{user.getEmail()}");
                    System.out.println("请问确认要删除吗？y or n");
                    String choice_input = in.next();
                    if (choice_input.equals("y")) {
                        list.remove(user);
                        System.out.println("删除成功！");
                        flag = true;
                        break;
                    }
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                System.out.println("未找到该会员");
            }
        }
        else
            System.out.println("您的输入不合法");
    }
    // 查询用户信息
    public static void queryUser(ArrayList<User> list) {
        Scanner in = new Scanner(System.in);
        System.out.println("您选择了查询客户功能");
        System.out.println("请选择输入Username或ID：1-ID，2-Username,3-查询所有客户");
        String choice = in.next();
        switch (choice) {
            case "1" -> {
                System.out.println("请输入ID：");
                String Id = in.next();
                boolean flag = false;

                for (User user : list) {
                    if (Id.equals(user.getid())) {
                        System.out.println("客户ID为" + user.getid() + "的客户信息为：" + STR."\{user.getid()}\t\{user.getUsername()}\t\{user.getLeval()}\{user.getRegisterTime()}\t\{user.getTotalAmount()}\t\{user.getPhoneNumber()}\t\{user.getEmail()}");
                        flag = true;
                        break;
                    }

                }

                if (!flag) {
                    System.out.println("未找到该客户");
                }

            }
            case "2" -> {
                System.out.println("请输入username：");
                String username = in.next();

                boolean flag = false;
                for (User user : list) {
                    if (username.equals(user.getUsername())) {
                        System.out.println("username为" + user.getUsername() + "的客户信息为：" + STR."\{user.getid()}\t\{user.getUsername()}\t\{user.getLeval()}\{user.getRegisterTime()}\t\{user.getTotalAmount()}\t\{user.getPhoneNumber()}\t\{user.getEmail()}");
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    System.out.println("未找到该客户");
                }
            }
            case "3" -> printUserList(list);//xiugai printlist
            case null, default -> System.out.println("您的输入不合法");
        }
    }

    // 打印商品列表
    public static void printProductList(ArrayList<Product> list3) {
        if(list3.isEmpty()){
            System.out.println("当前无商品信息，请添加后再查询");
            return;
        }
        System.out.printf("%-10s %-15s %-10s %-15s %-15s %-8s %-8s %-5s%n",
                "商品编号", "商品名称", "生产厂家", "生产日期", "型号", "进货价", "零售价格", "数量");
        for (Product pro : list3) {
            System.out.printf("%-10s %-15s %-15s %-15s %-20s %-10.2f %-10.2f %-10d%n",
                    pro.getProductId(),
                    pro.getProductName(),
                    pro.getManufacturer(),
                    pro.getPorductionDate(),
                    pro.getMdoel(),
                    pro.getPurchasePrice(),
                    pro.getRetailPrice(),
                    pro.getQuantity());
        }
    }
    // 增加商品
    public static void addProduct(ArrayList<Product> list3) {
        Product p=new Product();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商品编号");
        String pro=sc.next();
        p.setProductId(pro);
        System.out.println("请输入商品名称");
        String name=sc.next();
        p.setProductName(name);
        System.out.println("请输入生产厂家");
        String manufac=sc.next();
        p.setManufacturer(manufac);
        System.out.println("请输入生产日期");
        String date=sc.next();
        p.setPorductionDate(date);
        System.out.println("请输入型号");
        String model=sc.next();
        p.setMdoel(model);
        System.out.println("请输入进货价");
        double pur=sc.nextDouble();
        p.setPurchasePrice(pur);
        System.out.println("请输入零售价");
        double retail=sc.nextDouble();
        p.setRetailPrice(retail);
        System.out.println("请输入商品数量");
        int quantity=sc.nextInt();
        p.setQuantity(quantity);
        list3.add(p);
    }
    // 修改商品信息
    public static void modifyProduct(ArrayList<Product> list3) {
        Scanner sc = new Scanner(System.in);
        System.out.println("您选择了修改商品功能");
        System.out.println("请选择输入商品编号或商品名称：1-productId,2-productName");
        String choice = sc.next();
        if (Objects.equals(choice, "1")) {
            System.out.println("请输入Id：");
            String id = sc.next();
            boolean flag = false;

            for (Product pro : list3) {
                if (id.equals(pro.getProductId())) {

                    System.out.println("商品编号为" + pro.getProductId() + "的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");

                    System.out.println("请输入要修改的商品信息：1-商品编号 2-商品名称 3-生产厂家 4-生产日期 5-型号 6-进货价 7-零售价格 8-数量");
                    String choice_input = sc.next();
                    switch (choice_input) {
                        case "1":
                            String newId = sc.next();
                            pro.setProductId(newId);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "2":
                            String newname = sc.next();
                            pro.setProductName(newname);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "3":
                            String newmanu = sc.next();
                            pro.setManufacturer(newmanu);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "4":
                            String newdate = sc.next();
                            pro.setPorductionDate(newdate);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "5":
                            String newmod = sc.next();
                            pro.setMdoel(newmod);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "6":
                            double newpur = sc.nextDouble();
                            pro.setPurchasePrice(newpur);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "7":
                            double newretail = sc.nextDouble();
                            pro.setRetailPrice(newretail);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "8":
                            int newqua = sc.nextInt();
                            pro.setQuantity(newqua);
                            System.out.println("商品编号为" + pro.getProductId() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        default:
                            System.out.println("没有这个选项");

                    }
                    flag = true;
                    break;
                }
            }
                if(!flag){
                    System.out.println("未找到该客户");
            }
        }
            else if (Objects.equals(choice, "2")) {
                System.out.println("请输入商品名称：");
                String name = sc.next();
                boolean flag = false;

            for (Product pro : list3) {
                if (name.equals(pro.getProductName())) {

                    System.out.println("商品名称为" + pro.getProductName() + "的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");

                    System.out.println("请输入要修改的商品信息：1-商品编号 2-商品名称 3-生产厂家 4-生产日期 5-型号 6-进货价 7-零售价格 8-数量");
                    String choice_input = sc.next();
                    switch (choice_input) {
                        case "1":
                            String newId = sc.next();
                            pro.setProductId(newId);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "2":
                            String newname = sc.next();
                            pro.setProductName(newname);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "3":
                            String newmanu = sc.next();
                            pro.setManufacturer(newmanu);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "4":
                            String newdate = sc.next();
                            pro.setPorductionDate(newdate);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "5":
                            String newmod = sc.next();
                            pro.setMdoel(newmod);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "6":
                            double newpur = sc.nextDouble();
                            pro.setPurchasePrice(newpur);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "7":
                            double newretail = sc.nextDouble();
                            pro.setRetailPrice(newretail);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        case "8":
                            int newqua = sc.nextInt();
                            pro.setQuantity(newqua);
                            System.out.println("商品名称为" + pro.getProductName() + "修改后的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                            break;
                        default:
                            System.out.println("没有这个选项");

                    }
                    flag = true;
                    break;
                }
            }
                    if (!flag) {
                        System.out.println("未找到该商品");

                }
            }

            else
                System.out.println("您的输入不合法");



    }
    // 删除商品
    public static void removeProduct(ArrayList<Product> list3) {Scanner in = new Scanner(System.in);
            System.out.println("您选择了删除商品功能");
            System.out.println("请选择输入商品编号或商品名称：1-productId，2-productName");
            String choice = in.next();
            if (Objects.equals(choice, "1")) {
                System.out.println("请输入商品编号：");
                String Id = in.next();
                boolean flag = false;

                for (int i = 0; i < list3.size(); i++)  {
                    Product pro =list3.get(i);
                    if (Id.equals(pro.getProductId())) {

                        System.out.println("商品编号为" + pro.getProductId() + "的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                        System.out.println("删除后无法恢复，请确认要删除吗？y or n");
                        String choice_input = in.next();
                        if (choice_input.equals("y")) {
                            list3.remove(pro);
                            System.out.println("删除成功！");
                            flag = true;
                            break;
                        }
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    System.out.println("未找到该商品");
                }
            } else if (Objects.equals(choice, "2")) {
                System.out.println("请输入商品名称：");
                String name = in.next();

                boolean flag = false;
                for (int i = 0; i < list3.size(); i++){
                    Product pro=list3.get(i);
                    if (name.equals(pro.getProductName())) {
                        System.out.println("商品名称为" + pro.getProductName() + "的商品信息为：" + STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                        System.out.println("删除后无法恢复，请确认要删除吗？y or n");
                        String choice_input = in.next();
                        if (choice_input.equals("y")) {
                            list3.remove(pro);
                            System.out.println("删除成功！");
                            flag = true;
                            break;
                        }
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    System.out.println("未找到该商品");
                }
            }
            else
                System.out.println("您的输入不合法");
    }


    public static void searchProduct(ArrayList<Product>list3,String searchCriteria) {
        boolean found = false;

        for (Product product : list3) {
            if (product.getProductName().contains(searchCriteria) ||
                    product.getManufacturer().contains(searchCriteria)) {
                printProductDetails(product);
                found = true;
            } else if (isNumeric(searchCriteria)) {
                double numericValue = Double.parseDouble(searchCriteria);
                if (product.getRetailPrice() > numericValue) {
                    printProductDetails(product);
                    found = true;
                }
            }
        }
        if (!found) {
            System.out.println("No matching product found.");
        }
    }

    private static void printProductDetails(Product product) {
        System.out.println( "商品信息：" + STR."\{product.getProductId()}\t\{product.getProductName()}\t\{product.getManufacturer()}\t\{product.getPorductionDate()}\t\{product.getMdoel()}\t\{product.getPurchasePrice()}\t\{product.getRetailPrice()}\t\{product.getQuantity()}");
    }

    private static boolean isNumeric(String strNum) {
        if (strNum == null) {
            return false;
        }
        try {
            Double.parseDouble(strNum);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}
